<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title></title>
    <style type="text/css">
      * {
        padding: 0;
        margin: 0;
        list-style: none;
        border: 0;
      }

      .all {
        width: 500px;
        height: 200px;
        padding: 7px;
        border: 1px solid #ccc;
        margin: 100px auto;
        position: relative;
      }

      .screen {
        width: 500px;
        height: 200px;
        /* overflow: hidden; */
        position: relative;
      }

      .screen li {
        width: 500px;
        height: 200px;
        overflow: hidden;
        float: left;
      }

      .screen ul {
        position: absolute;
        left: 0;
        top: 0px;
        width: 3000px;
      }

      .all ol {
        position: absolute;
        right: 10px;
        bottom: 10px;
        line-height: 20px;
        text-align: center;
      }

      .all ol li {
        float: left;
        width: 20px;
        height: 20px;
        background: #fff;
        border: 1px solid #ccc;
        margin-left: 10px;
        cursor: pointer;
      }

      .all ol li.current {
        background: yellow;
      }

      #arr {
        display: none;
        z-index: 1000;
      }

      #arr span {
        width: 40px;
        height: 40px;
        position: absolute;
        left: 5px;
        top: 50%;
        margin-top: -20px;
        background: #000;
        cursor: pointer;
        line-height: 40px;
        text-align: center;
        font-weight: bold;
        font-family: "黑体";
        font-size: 30px;
        color: #fff;
        opacity: 0.3;
        border: 1px solid #fff;
      }

      #arr #right {
        right: 5px;
        left: auto;
      }
    </style>
  </head>

  <body>
    <div class="all" id="box">
      <div class="screen" id="screen">
        <ul id="ul">
          <li><img src="images/wf1.jpg" width="500" height="200" /></li>
          <li><img src="images/wf2.jpg" width="500" height="200" /></li>
          <li><img src="images/wf3.jpg" width="500" height="200" /></li>
          <li><img src="images/wf4.jpg" width="500" height="200" /></li>
          <li><img src="images/wf5.jpg" width="500" height="200" /></li>
          <!-- <li><img src="images/wf1.jpg" width="500" height="200" /></li> -->
        </ul>
        <ol id="ol"></ol>
      </div>
      <div id="arr"><span id="left">&lt;</span><span id="right">&gt;</span></div>
    </div>
    <script>
      function animate(obj, target, callback) {
        clearInterval(obj.timer);
        obj.timer = setInterval(function () {
          // 获取盒子的位置
          var currentPos = obj.offsetLeft;
          // 步长值
          var step = (target - currentPos) / 10;
          step = step > 0 ? Math.ceil(step) : Math.floor(step);
          // 限制盒子的移动距离
          if (currentPos === target) {
            callback && callback();
            clearInterval(obj.timer);
            return;
          }

          obj.style.left = currentPos + step + "px";
        }, 15);
      }
    </script>
    <script>
      // 1.获取ul和ol标签对象
      var ul = document.querySelector("#ul");
      var ol = document.querySelector("#ol");
      // 获取screen盒子的宽度
      var screenWidth = document.querySelector("#screen").offsetWidth;
      // 遍历 ul下面的li对象 通过遍历它确定 ol里面的li
      for (var i = 0; i < ul.children.length; i++) {
        // 创建li
        var li = document.createElement("li");
        li.innerHTML = i + 1;
        // 将创建的li追加到ol标签
        ol.appendChild(li);
        // 给第一个li添加类名 current
        if (i === 0) li.className = "current";
      }

      // 2. 遍历ol里面的li 给li添加点击事件
      var lis = ol.children;
      for (var i = 0; i < lis.length; i++) {
        // 先添加自定义属性
        lis[i].setAttribute("data-index", i);
        lis[i].addEventListener("click", function () {
          // 当鼠标点击li时 需要知道点击的是哪个li
          var index = this.getAttribute("data-index");
          // console.log(index);
          animate(ul, -index * screenWidth);
          for (var j = 0; j < lis.length; j++) {
            lis[j].className = "";
          }
          this.className = "current";
        });
      }
      var arr = document.querySelector("#arr");
      // 3.鼠标经过box盒子时让箭头显示
      box.addEventListener("mouseenter", function () {
        arr.style.display = "block";
      });
      // 4.鼠标离开box盒子时让箭头隐藏
      box.addEventListener("mouseleave", function () {
        arr.style.display = "none";
      });

     
    </script>
  </body>
</html>
